<?php
namespace app\atxunda\model;
use think\Model;
use think\Input;
use think\Cookie;

class Privilege extends Model
{
	//查询出所有数据
  	 public function getInfo(){
  	 	$data=Privilege::select();
  	 	return Privilege::noLimitCate($data,$parent_id=0,$level=0);
  	 }
  	 /*
  	  @方法名 无限级分类
  	  @function noLimitCate
  	  @param1 array $data
  	  @param2 int   $parent_id=0
  	  @param3 int   $level=0
      @return  $lists
  	  */
  	 public function noLimitCate($data,$parent_id=0,$level=0){
  	 	static $lists=array();
  	 	foreach ($data as $key => $v) {
  	 		if($v['parent_id']==$parent_id){
  	 			$v['level']=$level;
  	 			$lists[]=$v;
  	 			Privilege::noLimitCate($data,$v['privilege_id'],$level+1);
  	 		}
  	 	}
  	 	return $lists;
  	 }


     public function getPri(){
       //首先以pid查询顶级分类
       $arr = Privilege::where("parent_id=0")->select();
       foreach ($arr as $k => $v) {
         //以每条数据的p_id作为该权限下的子权限的pid
         $p_id = $v['privilege_id'];
         $data = Privilege::where("parent_id=$p_id")->select();
         $arr[$k]['child'] = $data;
       }
       return $arr;
     }


     /**
     * 获取权限
     * @return [type] [description]
     */
     public function getLeftPri(){
        $admin = unserialize(cookie('admin'));
        $admin_id = $admin['admin_id'];
        if($admin['role_id'] == 1){
          $data = Privilege::where(['parent_id'=>0,'privilege_status'=>1])->select();
          foreach ($data as $k => $v) {
            $arr = Privilege::where(['parent_id'=>$v['privilege_id'],'privilege_status'=>1])->select();
            $data[$k]['son'] = $arr;
          }
        }
        else{
          $privilegeIds = RolePrivilege::where('role_id','in',$admin['role_id'])->column('privilege_id');
          $data = Privilege::where('privilege_id','in',$privilegeIds)->where('privilege_status',1)->where('parent_id',0)->select();
          foreach ($data as $k => $v) {
            $arr = Privilege::where('privilege_id','in',$privilegeIds)->where('privilege_status',1)->where('parent_id',$v['privilege_id'])->select();
             $data[$k]['son'] = $arr;
          }
        }
        return $data;
      }

}